package org.se.scut.pinterest.mina.codec.filte.coder.encoder;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.apache.mina.filter.codec.demux.MessageEncoder;
import org.se.scut.pinterest.mina.codec.msg.AllPictureTypeResultMessage;

public class AllPictureTypeResultMessageEncoder extends BaseMessageEncoder 
	implements MessageEncoder<AllPictureTypeResultMessage> {

	@Override
	public void encode(IoSession session, AllPictureTypeResultMessage message,
			ProtocolEncoderOutput out) throws Exception {
		IoBuffer buffer = IoBuffer.allocate(200).setAutoExpand(true);
		buffer.putString(message.getSymbol() + delimiter, ENCODER);
		int size = message.getItems().size();
		buffer.putString(size + delimiter, ENCODER);
		
		for (int i = 0; i < size; i++) {
			buffer.putString(message.getItems().get(i).getId() + delimiter, ENCODER);
			buffer.putString(message.getItems().get(i).getTypeName() + delimiter, ENCODER);
			buffer.putString(message.getItems().get(i).getLastModifiedDate() + delimiter, ENCODER);
		}
		buffer.flip();
		out.write(buffer);
		
	}

}
